/**
 * Copyright 北京彩通农旗资讯有限公司 All rights reserved.
 * 北京彩通农旗资讯有限公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 */
package com.xspace.android.xafcommon.util.commonUtil;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.Iterator;
import java.util.Map;


public class SkipTools {
    private static String TAG = "SkipTools";

    public static void initModuleApk(final Context mContext, String key, final Map<String, Object> dataMap) {
//        final ProgressDialog dialogW = DialogHelp.getWaitDialog(mContext, "正在加载");
        ModuleBean targetBean = ModuleManager.getInstance().getModuleBeanByClassNamekey(key);
        ModuleBean.ToClass targetClass = null;
        if (targetBean == null)
            return;
        for (ModuleBean.ToClass toClass : targetBean.getClassList()) {
            if (key.equals(toClass.getKey())) {
                targetClass = toClass;
                break;
            }
        }
        if (targetClass == null) {
            Toast.makeText(mContext, "没有找到对应类", Toast.LENGTH_SHORT).show();
            return;
        }
        final String packName = targetBean.getPackageName();
        final String apkName = targetBean.getApkName();
        final String moduleName = targetBean.getModelName();
        final String mainClass = targetClass.getClassName();
        final int versionNew = Integer.parseInt(targetBean.getVersion());
        final String classType = targetClass.getClassType();
        LogUtil.d(TAG, "检测到已经安装......");
        //已经安装,更新去
        try {
            Class activityClass = Class.forName(mainClass);
            Intent intent = new Intent(mContext, activityClass);
            LogUtil.d("packName" + packName);
            LogUtil.d("mainClass" + mainClass);
            intent.putExtra(Constants.PACKAGE_NAME_MAIN, BaseApplication.mInstance.getPackageName());
            if (dataMap != null) {
                Iterator<Map.Entry<String, Object>> it = dataMap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    if (!"undefined".equals(entry.getValue()) && entry.getValue() != null) {
                        intent.putExtra(entry.getKey(), entry.getValue().toString());
                    }
                }
            }
            LogUtil.d(TAG, "拼接的数据....." + intent.getExtras().toString());
            if ("service".equals(classType)) {
                LogUtil.d(TAG, "跳转到服务......" + mainClass);
                mContext.startService(intent);
            } else {
                LogUtil.d(TAG, "跳转到activity......" + mainClass);
                mContext.startActivity(intent);
            }
        } catch (Exception e) {
            LogUtil.d(TAG, "跳转到a失败" + mainClass);
            Toast.makeText(mContext, moduleName + "模块还未安装，请耐心等待", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
//			}
//		}
//	else {
//			LogUtil.d(TAG,"检测到未安装......");
//			Toast.makeText(mContext,moduleName+"未安装,请稍后再试！",Toast.LENGTH_LONG).show();
//		}
        //未安装
//			DialogHelp.getMessageDialog(mContext, "首次使用需要安装" + moduleName + "模块，确认安装吗？", "提醒", "取消", "确定",
//					new OnClickListener() {
//
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							dialogW.show();
//							Toast.makeText(mContext, "这可能需要一小段时间，请耐心等待！", Toast.LENGTH_LONG).show();
//							new Thread(new Runnable() {
//								@Override
//								public void run() {
//									try {
//										Thread.sleep(500);
//									} catch (InterruptedException e) {
//										e.printStackTrace();
//									}
//									Tools.copyAndInstall(mContext, apkName);
//									dialogW.cancel();
//								}
//							}).start();
//						}
//					}, new OnClickListener() {
//
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//						}
//					}).create().show();
//		}
    }
}